const util = require('../../utils/util.js')
import { tim } from '../../message/message.js'
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        phone: '', //手机号
        authentication_state: 0, //认证状态
        bind_phone_back: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        //请求分享数据
        wx.request({
            url: app.globalData.ApiUrl + '/index.php?act=wx_share&op=get_share',
            data: {},
            method: 'GET',
            dataType: 'json',
            success: res => {
                if (res.data.code == '200') {
                    this.setData({
                        share_default_title: res.data.datas.title,
                        share_default_image_url: res.data.datas.imageUrl
                    })
                }
            }
        })
        util.share();
        var self = this;
        wx.getStorage({
            key: 'key',
            success: (res) => {
                self.setData({
                    'member_key': res.data,
                });
                self.getPhoneInfo();
                self.getAuthenticationInfo();
            }
        })
    },
    /**
     * 获取手机号码信息
     */
    getPhoneInfo: function() {
        var self = this;
        wx.request({
            url: app.globalData.ApiUrl + '/index.php?act=member_account&op=get_mobile_info&key=' + self.data.member_key,
            success(res) {
                if (res) {

                }
                self.setData({
                    phone: res.data.datas.mobile
                });
            }
        });
    },
    /**
     * 获取认证信息
     */
    getAuthenticationInfo() {
        var self = this;
        wx.request({
            url: app.globalData.ApiUrl + '/index.php?act=member_account&op=real_name_state&key=' + self.data.member_key,
            success(res) {
                self.setData({
                    authentication_state: res.data.datas.member_certification_state
                });
            }
        });
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        let self = this;
        if (self.data.bind_phone_back) {
            self.getPhoneInfo();
            self.data.bind_phone_back = false;
        }
    },
    exitLogin: function() {
        try {
            wx.clearStorage();
            wx.switchTab({
                url: '../member/member'
            });
        } catch (e) {
            // Do something when catch error
        }
    },
    delCache: function() {
        let self = this;
        wx.showModal({
            content: '确定要清除缓存吗？',
            success(res) {
                if (res.confirm) {
                    wx.getStorageInfo({
                        success(res) {
                            let arr = ["key", "username", "member_mobile", "cart_count", "packageTime", "_mta_ssid", "latitude", "longitude", "address", "share_key"];
                            for (var i = 0; i < res.keys.length; i++) {
                                var inArray = self.in_array(res.keys[i], arr);
                                if (!inArray) {
                                    wx.removeStorage({
                                        key: res.keys[i],
                                        success(res) {}
                                    })
                                }
                            }
                            wx.showToast({
                                title: '清除缓存成功',
                                icon: 'success',
                                duration: 2000
                            })
                        }
                    })
                }
            }
        })
    },
    in_array: function(search, array) {
        for (var i in array) {
            if (array[i] == search) {
                return true;
            }
        }
        return false;
    },
    onShareAppMessage: function() {
        let share_key = this.data.key ? this.data.key : '';
        let share_path = '';
        if (util.getCurrentPageUrlWithArgs().indexOf("?") != -1) {
            share_path = util.getCurrentPageUrlWithArgs() + '&share_key=' + share_key;
        } else {
            share_path = util.getCurrentPageUrlWithArgs() + '?share_key=' + share_key;
        }
        return {
            title: this.data.share_default_title,
            imageUrl: this.data.share_default_image_url,
            path: share_path
        }
    }
})